import prisma from '~~/lib/prisma'
import Joi from 'joi'
import { Result } from '~~/lib/helper/result'

interface Item {
  id: number
  name: string
}
export default defineEventHandler(async (event) => {
  const body: Item = await readBody(event)
  const schema = Joi.object<Item>({
    id: Joi.number().required(),
    name: Joi.string().required(),
  })

  try {
    await schema.validateAsync(body)
  } catch (error) {
    return Result.Error('参数错误！')
  }
  try {
    await prisma.notebook.update({
      where: { id: body.id },
      data: { name: body.name },
    })
  } catch (error) {
    return Result.Error('更新文集失败！')
  }
  return Result.Ok(null, 200, '更新文集成功！')
})
